home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4081 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.3 KB

  1. Path: news.princeton.edu!blume
  2. From: blume@zayin.cs.princeton.edu (Matthias Blume)
  3. Newsgroups: comp.lang.c++,comp.lang.c
  4. Subject: Re: Performance: C vs. C++
  5. Date: 27 Jan 1996 20:26:10 GMT
  6. Organization: Princeton University
  7. Distribution: world
  8. Message-ID: <BLUME.96Jan27152610@zayin.cs.princeton.edu>
  9. References: <30F6BAAC.12B5@iastate.edu> <4da9pn$a45@news.bridge.net>
  10.     <4dnpl2$c8g@classic.iinet.com.au> <3105E9DC.1BE3@enermet.fi>
  11.     <DLr46y.7rH@txnews.amd.com>
  12. NNTP-Posting-Host: zayin.cs.princeton.edu
  13. In-reply-to: Bret Patterson's message of Thu, 25 Jan 1996 19:16:09 GMT
  14.  
  15. In article <DLr46y.7rH@txnews.amd.com> Bret Patterson <faustus> writes:
  16.  
  17.    Here is an example to illustrate why virtual functions do not cause extra overhead.
  18.  
  19.    C program:
  20.  
  21.    switch (object->type)
  22.    {
  23.        case 1 : DragonAttack();
  24.        case 2 : OrcAttack();
  25.        ...
  26.    };
  27.  
  28.    and a C++:
  29.        object->Attack(); // which internally g++ does a object->virtualTable[Function#]();
  30.  
  31.    Big deal. So c++ does a virtual table lookup, but in order for C to have this ability
  32.    it has to use a switch statement or ifelse construct.
  33.  
  34. Why?  You just gave the _real_ C equivalent yourself!  A
  35. multi-branch switch was never the C way of doing virtual functions.
  36. C++'s virtual functions are nothing more than glorified C function
  37. pointers.
  38.  
  39. --
  40. -Matthias
  41.